package com.xxdai.ace.persistence.hbase.fun;

import com.inns.position.hbase.util.HbaseUtil;
import com.xxdai.ace.persistence.hbase.jdbc.coprocessor.client.ExpandAggregationClient;
import com.xxdai.ace.persistence.hbase.jdbc.coprocessor.client.bean.ExpandHbaseCell;
import com.xxdai.ace.persistence.hbase.jdbc.coprocessor.client.bean.ExpandHbaseCondition;
import com.xxdai.ace.persistence.hbase.jdbc.coprocessor.client.bean.ExpandHbaseRow;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;

import java.util.ArrayList;
import java.util.List;

//import com.xxdai.ace.persistence.hbase.jdbc.template.dao.HbaseTemplateBaseDao;

/**
 * Copyright (c) 2014, xxxxxx All Rights Reserved.
 * Package: com.xxdai.ace.persistence.hbase.fun
 * <p>
 * File: CountDemo.java
 * <p>
 * Author: lpy
 * Date: 2016年1月8日
 * <p>
 * Copyright @ 2016 Corpration Name
 */
public class CountDemo {

    public static void main(String[] args) {
        try {
            Scan s = new Scan();

            ExpandAggregationClient ac = new ExpandAggregationClient(HbaseUtil.getConfiguration());
            s.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"));
            s.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));
            s.addColumn(Bytes.toBytes("info"), Bytes.toBytes("number"));
//            AggregationClient ac1 = new AggregationClient(hbaseTemplate.getConfiguration());
            Long lo = 0L;
            ExpandHbaseCondition hbase = new ExpandHbaseCondition();
            List<ExpandHbaseCell> sumList = new ArrayList<ExpandHbaseCell>();
            ExpandHbaseCell cell = new ExpandHbaseCell("info", "number", "Long");
            sumList.add(cell);
            hbase.setSumList(sumList);
            cell = new ExpandHbaseCell("info", "name");
            // ExpandHbaseCell cell2 = new ExpandHbaseCell("info", "age");
            List<ExpandHbaseCell> group = new ArrayList<ExpandHbaseCell>();
            group.add(cell);
            //group.add(cell2);
            hbase.setGroupList(group);
            cell = new ExpandHbaseCell("info_count", "age");
            List<ExpandHbaseCell> count = new ArrayList<ExpandHbaseCell>();
            count.add(cell);
            hbase.setCountList(count);
            //hbase.setDistinctCountList(group);
            List<ExpandHbaseCell> distinct = new ArrayList<ExpandHbaseCell>();
            ExpandHbaseCell cell1 = new ExpandHbaseCell("info", "age");
            ExpandHbaseCell cell2 = new ExpandHbaseCell("info", "name");
            distinct.add(cell1);
            distinct.add(cell2);
            hbase.setDistinctCountList(distinct);
            List<ExpandHbaseRow> result = null;
            try {
                //lo = ac1.rowCount(tableName, new LongColumnInterpreter(), s);
                result = ac.getGroupSumAndCount("demoTest", hbase, s);
                System.out.println(result == null ? "bug" : result.size());
                //System.out.println(lo);
                for (ExpandHbaseRow row : result) {
                    String context = "";
                    List<ExpandHbaseCell> ll = row.getResultRow();
                    if (ll != null)
                        for (ExpandHbaseCell c : ll) {
                            context += c.getFamily() + ":" + c.getQualify() + "--->";
                            context += c.getValue();
                            context += "\t";
                        }
                    System.out.println(context);

                }

                result = ac.getDistinctCount("demoTest", hbase, s);
                System.out.println(result == null ? "bug" : result.size());
                for (ExpandHbaseRow row : result) {
                    String context = "";
                    List<ExpandHbaseCell> ll = row.getResultRow();
                    if (ll != null)
                        for (ExpandHbaseCell c : ll) {
                            context += c.getFamily() + ":" + c.getQualify() + "--->";
                            context += c.getValue();
                            context += "\t";
                        }
                    System.out.println(context);
                }
            } catch (Throwable e) {
                e.printStackTrace();
            }
            System.out.println(lo);
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

}

